<html>
<title>XSocial Test</title>
<meta charset="utf-8">
<head>
<script type="text/javascript" src="<?php echo $this->webRoot ?>js/xsocialRpc.js"></script>
<?php
require_once 'lib/renren-api-php-sdk/RenRenClient.class.php';
$config->APIKey = $this->platParams['apikey'];
$config->SecretKey = $this->platParams['secret'];
$rrObj = new RenRenClient($config);
$rrObj->setSessionKey($_REQUEST['xn_sig_session_key']);
$ret = $rrObj->POST('users.getLoggedInUser');
$userid = $ret["uid"];
$user = $rrObj->POST('users.getInfo', array($userid,'uid,name,tinyurl,headurl,sex,birthday'));
$user = formatUser($user[0]);
$sessionkey = time();
$sig = md5($sessionkey . $userid . $this->secretKey);
$friends = $rrObj->POST('friends.getFriends', array('',''));
$out_friends = array();
foreach($friends as $friend) {
    $out_friends[] = formatUser($friend);
}

function formatUser($user) {
    $temp_user = array();
    $temp_user['uid'] = $user['uid'];
    if (empty($temp_user['uid'])) $temp_user['uid'] = $user['id'];
    $temp_user['name'] = $user['name'];
    if (empty($user['headurl'])) {
        $temp_user['thumbnail'] = $user['tinyurl'];
    } else {
        $temp_user['thumbnail'] = $user['headurl'];
    }
    $temp_user['gender'] = $user['sex'];
    $temp_user['birthday'] = $user['birthday'];
    return $temp_user;
}

?>
<head>
<body>
<div id="xsocial-container"></div>
<script type="text/javascript" src="http://static.connect.renren.com/js/v1.0/FeatureLoader.jsp"></script>
<script type="text/javascript">
XN_RequireFeatures(["Connect","CanvasUtil"], function()
{
  XN.Main.init("<?php echo $this->platParams['apikey']; ?>", "<?php echo $this->webRoot?>template/renren/xd_receiver.html");
  xsocialApi.init();
});

</script>
<script type="text/javascript"> 
var xsocialTestApi = {
    init : function() {
        var viewer = <?php echo json_encode($user) ?>;
        xsocialTestApi.uid = viewer.uid;
        xsocialTestApi.sessionkey = <?php echo $sessionkey ?>;
        xsocialTestApi.sig = "<?php echo $sig ?>";
        initRpc();
    },
    
    getViewer : function(callback) {
        var viewer = <?php echo json_encode($user) ?>;
        if (callback) callback(viewer);
    },
    
    getFriends : function(callback,opt) {
        var friends = <?php echo json_encode($out_friends) ?>;
        xsocialTestApi.friends = friends;
        if (callback) callback(friends);
    },
    
    invite : function(bundleData,callback) {
        var appid = <?php echo $this->platParams['appid']; ?>;
        var appname = "<?php echo $this->platParams['appname']; ?>";
        if (appid == null || appname == null) {
            alert('to use invite you must first set appid and appname in platParams');
            return;
        }
        top.location.href = "http://apps.renren.com/invite.do?app_id=" + appid +"&returnUrl=" + "http://apps.renren.com/" + appname;
    },
    payment : function() {
        alert("payment not implemented.");
    },
    adjustHeight : function(height) {
        XN.CanvasClient.setCanvasHeight((height + 20) + "px" );
    },
    post : function(bundleData, callback) {
        if (bundleData.id == null) {
            bundleData.id = 1;
        }
        var feedSettings = {
                "template_bundle_id": bundleData.id,
                "template_data":{
                    "images":[{"src":bundleData.pic, "href":bundleData.link}],
                    "content":bundleData.content,
                    "title" : bundleData.title
                    },
                "callback": callback,
                "body_general": "",
                "user_message_prompt": bundleData.title,
                "user_message": ""
            };
            XN.Connect.showFeedDialog(feedSettings);
    }
};
var xsocialApi = xsocialTestApi;
var ifrSrc = "<?php echo $this->appIframeUrl ?>";
</script>
</body>
</html>